/**
 * @BelongsProject: xx-club
 * @BelongsPackage: com.wx.subject.infra.basic.utils
 * @Author: wangx
 * @CreateTime: 2024-03-05  10:47
 * @Description: TODO
 * @Version: 1.0
 */
package com.wx.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * 数据库加密工具类
 */
public class DruidEncryptUtil {
    private static String publicKey; // 公钥
    private static String privateKey; // 私钥

    static {
        // 初始化公钥和私钥
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey= keyPair[0];
            publicKey = keyPair[1];
            System.out.println("公钥：" + publicKey);
            System.out.println("私钥：" + privateKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     *  加密
     * @param password 明文
     * @return 密文
     */
    public static String encrypt(String password) throws Exception {
        // 加密逻辑
        // ...
        String encrypt = ConfigTools.encrypt(privateKey, password);
        return encrypt;
    }
    /**
     * 解密
     * @param password 密文
     * @return 解密后的密码
     */
    public static String decrypt(String password) throws Exception {
        // 解密逻辑
        // ...
        String decrypt = ConfigTools.decrypt(publicKey, password);
        return decrypt;
    }

    public static void main(String[] args) {
        try {
            String password = "w20010125";
            String encrypt = encrypt(password);
            System.out.println("加密后的密码：" + encrypt);
            String decrypt = decrypt(encrypt);
            System.out.println("解密后的密码：" + decrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


